﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Facebook;

namespace FacebookStatus.Pages
{
    public partial class FacebookLogoutPage : PhoneApplicationPage
    {
        private const string AppId = "489399357833287";

        /// <summary>
        /// Extended permissions is a comma separated list of permissions to ask the user.
        /// </summary>
        /// <remarks>
        /// For extensive list of available extended permissions refer to 
        /// https://developers.facebook.com/docs/reference/api/permissions/
        /// </remarks>
        private const string ExtendedPermissions = "user_about_me,read_stream,publish_stream,user_status";
        private readonly FacebookClient _fb = new FacebookClient();
        public FacebookLogoutPage()
        {
            InitializeComponent();
        }

        private void webBrowser1_Navigated(object sender, NavigationEventArgs e)
        {

        }

        private void webBrowser1_Loaded(object sender, RoutedEventArgs e)
        {
            var logoutUrl = GetFacebookLogoutUrl(AppId, ExtendedPermissions);
            webBrowser1.Navigate(logoutUrl);
        }

        private Uri GetFacebookLogoutUrl(string appId, string extendedPermissions)
        {
            var parameters = new Dictionary<string, object>();
            parameters["client_id"] = appId;
            //parameters["redirect_uri"] = "https://www.facebook.com/connect/login_success.html";
            //parameters["response_type"] = "token";
            parameters["display"] = "touch";

            // add the 'scope' only if we have extendedPermissions.
            if (!string.IsNullOrEmpty(extendedPermissions))
            {
                // A comma-delimited list of permissions
                //parameters["scope"] = extendedPermissions;
            }
            return _fb.GetLogoutUrl(parameters);
        }
    }
}